package com.yuji.aacar.controller.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.TestActivity;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.bean.response.LoginResponse;
import com.yuji.aacar.controller.forgetpassword.ForgetPasswordActivity;
import com.yuji.aacar.controller.main.TabHomeActivity;
import com.yuji.aacar.controller.register.RegisterFristActivity;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.core.mvvm.appbase.AppRoute;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.utils.UmengUtils;
import com.yuji.aacar.widgets.autolayout.AutoLinearLayout;
import com.yuji.aacar.widgets.edittext.CheckEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <li>Package:com.yuji.aacar.controller.login</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/2/25</li>
 * <li>Description:   </li>
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.login_register)
    TextView loginRegister;
    @BindView(R.id.login_forget_password)
    TextView loginForgetPassword;
    @BindView(R.id.root_view)
    AutoLinearLayout rootView;
    @BindView(R.id.test)
    PingFangTextView test;
    @BindView(R.id.login_phone_number)
    CheckEditText loginPhoneNumber;
    @BindView(R.id.clear)
    ImageView clear;
    @BindView(R.id.login_password)
    CheckEditText loginPassword;
    @BindView(R.id.clear_password)
    ImageView clearPassword;

    private String phone;
    private String password;

    private LoginActivityVM loginActivityVM;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBarColor(R.color.white);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setHomePageAndExitApp(true);
        Tools.controlKeyboardLayout(this, rootView, loginBtn);

//        CustomDialog coreProgressDialog = new CustomDialog(this,"加载中...");
//        coreProgressDialog.show();

        initEdittextListener();
    }

    private void initEdittextListener() {
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginPhoneNumber.setText("");
            }
        });
        clearPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginPassword.setText("");
            }
        });
        loginPhoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0) {
                    clear.setVisibility(View.VISIBLE);
                } else {
                    clear.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        loginPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0) {
                    clearPassword.setVisibility(View.VISIBLE);
                } else {
                    clearPassword.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
        loginActivityVM = (LoginActivityVM) coreBaseViewModel;
        //        requestParams =
    }

    @OnClick({R.id.clear_password, R.id.login_phone_number, R.id.clear, R.id.login_password, R.id.login_btn, R.id.login_register, R.id.login_forget_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_phone_number:
                break;
            case R.id.clear:
                break;
            case R.id.login_password:
                break;
            case R.id.clear_password:
                break;
            case R.id.login_btn:

//                AppRoute.getInstance().nextController(this, TabHomeActivity.class, AppRoute.WITHOUT_RESULTCODE);
//                this.finish();
                if (isLegal()){
                    requestParams = BaseServiceParams.login(phone,password);
                    doTask(BaseServiceMediator.LOGIN);
                }
                break;
            case R.id.login_register:
                presentController(RegisterFristActivity.class);
                break;
            case R.id.login_forget_password:
                presentController(ForgetPasswordActivity.class);
                break;
        }
    }

    private boolean isLegal() {
        phone = loginPhoneNumber.getText().toString();
        password = loginPassword.getText().toString();

//        phone ="13764799336";
//        password ="123456";
        if (!Tools.isPhoneNumberValid(phone)){
            showToast("请输入正确的手机号！");
            return false;
        }
        if (!Tools.isAccountStandard(password)){
            showToast("密码为6~16位数字或英文！");
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.test)
    public void onClick() {
        presentController(TestActivity.class);
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.LOGIN_CODE){
            LoginResponse user = (LoginResponse) response.getResponse();
            UserCenter.login();
            UserCenter.saveCurrentUser(user);
            UmengUtils.onLogin(user.getMemberId());
            requestBindTokens();
        } else if (response.getRequestCode() == BaseServiceMediator.BIND_TOKEN_CODE){
            AppRoute.getInstance().nextController(this, TabHomeActivity.class, AppRoute.WITHOUT_RESULTCODE);
            this.finish();
        }
    }

    private void requestBindTokens() {
        String token = UserCenter.getDeviceTokens();
        LogUtil.e("device token-", token);
        requestParams = BaseServiceParams.update_devicetokens(UserCenter.getDeviceTokens());
        doTask(BaseServiceMediator.UPDATE_DEVICETOKEN);
    }

}
